public with sharing class InventoryController 
{
    public List<Item_Group__c> ItemGroups
    {
        get
        {
            ItemGroups = [SELECT Name,Description__c,Total_Inventory__c FROM Item_Group__c LIMIT 50000];
            return ItemGroups;
        }set;}
    public String NumberOfItemsToOrder{get;set;}
    public String NumberOfItemsToRemove{get;set;}
    public String SelectedItem{get;set;}
    public String SelectedItemGroup{get;set;}
    public List<SelectOption> ItemGroupsList{get;set;}
    public List<SelectOption> ItemsList{get;set;}
    public Boolean IsCertDataVisible{get;set;}
    public Boolean IsStockVisible{get;set;}
    public Boolean IsItemVisible{get;set;}
    public Item_Group__c CurrentItemGroup{get;set;}
    public Item__c CurrentItem{get;set;}
    public Certificate_Data__c CertData{get;set;}
    List<Item__c> items;
    
    public InventoryController()
    {
        ItemGroupsList = new List<SelectOption>();
        ItemGroupsList = getItemGroupsList(ItemGroups);    
    }
    public List<SelectOption> getItemGroupsList(List<Item_Group__c> itemGroups)
    {
        List<SelectOption> tempItemGroups = new List<SelectOption>();
        for(Item_Group__c itemGroup : itemGroups)
        {
            tempItemGroups.add(new SelectOption(itemGroup.id,itemGroup.name));
        }
        return tempItemGroups;
    }
    
    public void getItemsList() 
    {
        ItemsList = new List<SelectOption>();
        Items = [SELECT Name,Description__c,Cartridge_Colour__c,Item_Group__c,Number_of_Pins__c,Processor_Speed__c,Serial_Number__c,Version__c FROM Item__c WHERE Item_Group__c=:SelectedItemGroup];
        
        if(items!=null)
        {
            for(Item__c itm : items)
            {
               ItemsList.add(new SelectOption(itm.id,itm.name)); 
            }
            IsStockVisible = true;
        }
        else{
            IsStockVisible = false;
        }
        
        getCurrentItemGroup();
    }
    
    public void getItem() 
    {
        IsCertDataVisible = false;
        for(Item__c itm : Items)
        {
            if(SelectedItem!='None' && itm.id==SelectedItem)
            {
                CurrentItem = itm;
                List<Certificate_Data__c > CertDataList = [SELECT Name,id,Expiry_date__c,Pass__c,Test_Date__c FROM Certificate_Data__c WHERE Item__c =: SelectedItem];
                if(CertDataList!=Null && CertDataList.size()>0)
                {
                    CertData = CertDataList[0];
                    IsCertDataVisible = true;
                }
            }
        } 
        
    }
    
    public void getCurrentItemGroup()
    {
        for(Item_Group__c itmGroup : ItemGroups)
        {
            if(itmGroup.id==SelectedItemGroup)
            {
                CurrentItemGroup = itmGroup;
            }
        }
    }
    
    public void OrderItems()
    {
        try
        {
        if(NumberOfItemsToOrder!=null && SelectedItemGroup!=null)
        {
            Integer numberOfItems = Integer.ValueOf(NumberOfItemsToOrder);
            if(numberOfItems > 0 && numberOfItems <101)
            {
                Order__c order = new Order__c();
                
                    order.Status__c = 'New';
                    order.Units_Ordered__c = numberOfItems;
                    order.Item_Group__c = SelectedItemGroup;
                    insert order;
               
                     Approval.ProcessSubmitRequest orderReq = new Approval.ProcessSubmitRequest();
                     orderReq .setComments('Submitting order request for approval.');
                     orderReq .setObjectId(order.id);
                     Approval.ProcessResult result = Approval.process(orderReq);
                     if(result.isSuccess())
                     {
                         ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'Order sent for approval');
                         ApexPages.addMessage(myMsg); 
                     }
                     else
                     {
                         ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'Approval Process Failed');
                         ApexPages.addMessage(myMsg); 
                     }
                }
                else
                {
                    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'Please Enter a number between 1 - 100.');
                    ApexPages.addMessage(myMsg);  
                }
        }
        }
        catch(Exception ex)
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Data Insertion Failed.');
            ApexPages.addMessage(myMsg);
            System.debug(ex);  
        }
    }
    
    public void RemoveItems()
    {
        if(SelectedItemGroup != null && SelectedItem != null)
        {
        try{
               CurrentItemGroup.Total_Inventory__c = CurrentItemGroup.Total_Inventory__c - 1;
               
               if(CurrentItemGroup.Total_Inventory__c < 0.0)
               {
                   ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'You do not have enough stock to remove');
                   ApexPages.addMessage(myMsg);
               }
               else
               {
                   //CurrentItemGroup.Total_Inventory__c = tempTotalInventoryNumber;
                   update CurrentItemGroup;
                   delete CurrentItem;
                   ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'Item removed from the stock');
                   ApexPages.addMessage(myMsg);
               }  
        }
        catch(Exception ex)
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Data Update Failed');
            ApexPages.addMessage(myMsg); 
            System.debug(ex);
        }
        }
        else
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'Please select Item group and Item to remove');
            ApexPages.addMessage(myMsg);
        }
    }
}